package com.yunshan.system.controller;

import java.util.concurrent.TimeUnit;
import com.yunshan.common.annotation.RepeatSubmit;
import com.yunshan.common.core.domain.AjaxResult;
import com.yunshan.common.utils.UserUtils;
import com.yunshan.mp.domain.WechatFeedback;
import com.yunshan.mp.service.IWechatFeedbackService;
import com.yunshan.wechat.domain.UserInfoVo;
import com.yunshan.wechat.service.IWechatUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import static com.yunshan.common.core.domain.AjaxResult.*;

/**
 * 小程序系统设置
 *
 * @author yunshan
 * @date 2025-02-07
 */
@Api(tags = "小程序系统设置")
@RestController
@RequestMapping("/wx/system")
public class SystemController {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private IWechatFeedbackService wechatFeedbackService;
    @Autowired
    private IWechatUserInfoService wechatUserInfoService;


    /**
     * 退出登录
     */
    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public void logout(@RequestHeader("Authorization") String token) {
        String jwt = token.substring(7);
        redisTemplate.opsForValue().set("jwt:blacklist:" + jwt, "1", 2, TimeUnit.HOURS);
    }


    @ApiOperation("意见反馈")
    @RepeatSubmit
    @PostMapping("feedback")
    private AjaxResult feedback(
            @ApiParam(name = "feedback", value = "意见反馈对象", required = true)
            @RequestBody WechatFeedback feedback)
    {
        Long userId = UserUtils.getUserId();
        if (userId == null) {
            return AjaxResult.error("用户登录异常");
        }
        UserInfoVo userInfoVo = wechatUserInfoService.selectUserInfoById(userId);
        if (userInfoVo == null) {
            return AjaxResult.error("用户信息异常");
        }

        feedback.setUserId(userId);
        feedback.setPhone(userInfoVo.getPhone());
        feedback.setName(userInfoVo.getName());
        return success(wechatFeedbackService.insertWechatFeedback(feedback));
    }

}
